//
//  InitViewController.m
//  FlavorBase
//
//  Created by n22 on 15/3/12.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#import "InitViewController.h"
#import "LoginViewController.h"

@interface InitViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
//    UIPageControl *_pageControl;
}
@end

@implementation InitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建ScrollView
    [self createScrollView];
    //填充数据
    [self fitDataInScrollView];
    
}
#pragma mark -创建scrollView
- (void)createScrollView
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled =YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
//    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenSize.height-50, self.view.bounds.size.width, 50)];
//    _pageControl.backgroundColor = [UIColor clearColor];
//    _pageControl.numberOfPages = 7;
//    _pageControl.pageIndicatorTintColor = [UIColor orangeColor];
//    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//    //点击pageCountrol左侧 向左翻页 点击右侧 向右翻页
//    [_pageControl addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventValueChanged];
//    [self.view addSubview:_pageControl];

}
#pragma mark - 填充数据
- (void)fitDataInScrollView
{
    //创建一排图片视图 粘贴到滚动视图上
    for (int i =1; i<= 5; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenSize.width*(i-1), 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"引导页%d.png",i]];
        [_scrollView addSubview:imageView];
    }
    //设置内容大小
    _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * 7, _scrollView.bounds.size.height);
}


#pragma mark - PageControl触发之后要改变 滚动视图的内容偏移量
//- (void)pageClick:(UIPageControl *)page{
//    //更改内容的可视范围
//    [_scrollView scrollRectToVisible:CGRectMake(page.currentPage*_scrollView.bounds.size.width, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height) animated:YES];
//    [self pushRootViewController];
//}

#pragma mark - 减速到静止的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //获取当前的内容的偏移量
    CGPoint point = scrollView.contentOffset;
    NSInteger currentPage = point.x/_scrollView.bounds.size.width;
    [self pushRootViewController:currentPage];
}
- (void)pushRootViewController:(NSInteger)currentPage{
    if (currentPage == 4) {//判断是否到最后一页
        [self performSelector:@selector(setRootVC) withObject:nil afterDelay:1];
    }
}
- (void)setRootVC{
    self.view.window.rootViewController  = [[UINavigationController alloc]initWithRootViewController:[LoginViewController new]];;
}

@end
